####################################
# SCons build system for Pedigree
## Tyler Kennedy (AKA Linuxhq AKA TkTech)
####################################

import os
Import(['env'])

# Change to then name of this module
basedir = Dir("#").abspath
builddir = "#build"
kerneldir = Dir("#" + env["PEDIGREE_BUILD_KERNEL"]).abspath
objname = builddir + '/bootloader'
uimage = builddir + '/uImage.bin'
ramdisk = builddir + '/ramdisk.gz'
kernel = kerneldir + '/kernel'
initrd = env.File("#" + env["PEDIGREE_BUILD_BASE"] + "/initrd.tar")

env.Alias("kernel", objname)
env.Alias("kernel", uimage)
# env.Alias("kernel", ramdisk)
env.Depends(objname, kerneldir + "/kernel")

# To include a new subdirectory just add to the list.
subdirs = [
    
]

# To include new files just add to the list, or Glob("*.cpp")
files = [
    'Elf32.cc',
    'arm_support.s'
]

if env['arm_beagle']:
    files += ['main_beagle.cc']

# To include a new directory for includes, just add it to the list
include = [
    '.'
]

# To add a library add to the list below
libraries = [
]

# To add a library path add to the list below
libpaths = [
    env['LIBGCC']
]

####################################
SConscript([os.path.join(i, 'SConscript') for i in subdirs],exports = ['env'])
env.objects = [env.Object(i) for i in files]
env['CPPPATH'] = [i for i in include]
env['LIBS'] = [i for i in libraries]
env['LIBPATH'] = [i for i in libpaths]
#^-- Stupid way of doing it but I plan on adding processing.

def makeAutogenH(target, source, env):
    f = open(target[0].path, "w")
    f.write("/* Made from " + source[0].path + " */\n")
    f.write("/* Autogenerated by the build system, do not edit. */\n")
    
    f.write("uint8_t file[] = {\n");
    i = open(source[0].path, "rb")
    data = i.read()
    for i in data:
        f.write("0x%02x,\n" % (ord(i)))
    f.write("};\n")
    f.close()

env.Command("autogen.h", kerneldir + "/kernel", Action(makeAutogenH, "Building autogen.h..."))

env['CPPPATH'] += [os.path.dirname(env.File('autogen.h').path)]

if not len(files) == 0:
    #^-- Prevents 'no input files'
    build = env.Program(objname,env.objects,LINKFLAGS="-nostdlib -T " + basedir + "/src/system/boot/arm/link-arm-beagle.ld")

if env['arm_beagle']:
    # Set up the uboot image
    # filesystem, firmware, kernel, multi, ramdisk, script, standalone
    kernelType = "linux"
    loadAddr = "0x87000000"
    env.Command(uimage, objname, env['OBJCOPY'] + ' -O binary ' + env.File(objname).abspath + ' ' + env.File(objname).abspath + '.flt && mkimage -A arm -O ' + kernelType + ' -T kernel -C none -a ' + loadAddr + ' -e ' + loadAddr + ' -n "pedigree" -d ' + env.File(objname).abspath + '.flt ' + env.File(uimage).abspath + ' && cp ' + env.File(uimage).abspath + ' ' + env['uimage_target'])
    # env.Command(ramdisk, initrd, 'mkimage -A arm -O ' + kernelType + ' -T ramdisk -C gzip -a 0 -e 0 -n "pedigree-ramdisk" -d ' + env.File(initrd).abspath + ' ' + env.File(ramdisk).abspath + ' && cp ' + env.File(ramdisk).abspath + ' ' + env['uimage_target'])

